import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class Calulator extends JFrame implements ActionListener {
    private JTextField textField;

    public Calulator() {
        setTitle("我的计算器");
        setSize(400,400);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        JPanel panelNorth = new JPanel();
        JPanel panelCenter = new JPanel();
        add(panelNorth, BorderLayout.NORTH);
        add(panelCenter,BorderLayout.CENTER);

        //北面版
        textField = new JTextField();
        JButton clearButton = new JButton("清除");
        clearButton.addActionListener(this);
        panelNorth.setLayout(new BorderLayout());
        panelNorth.add(textField,BorderLayout.CENTER);
        panelNorth.add(clearButton,BorderLayout.EAST);
        //中面板
        String[] btnStr={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
        panelCenter.setLayout(new GridLayout(4,4));
        for (String s:btnStr) {
            JButton btn = new JButton(s);
            btn.addActionListener(this);
            panelCenter.add(btn);
        }
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Calulator();
            }
        });
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        String txt = e.getActionCommand();
        if("0.123456789+-*/".indexOf(txt)>=0){
            /*textField.setText(textField.getText()+txt);*/
            textField.setText(textField.getText()+txt);
        }
        else if (txt.equals("清除")) {
            textField.setText("");
        }
        //
        else if(txt.equals("=")) {
            //执行计算
            Map<String,Integer> pri = new HashMap<>();
            pri.put("-",1);
            pri.put("+",1);
            pri.put("*",2);
            pri.put("/",2);
            String exp = textField.getText();
            String[] numbers = exp.split("[\\+\\-\\*\\/]");
            String[] ops = exp.split("[0-9]+");
            ops= Arrays.copyOfRange(ops,1,ops.length);
            Stack<Integer> stack1 = new Stack<>();//创建堆
            Stack<String> stack2= new Stack<>();
            int len = numbers.length;
            stack1.push(Integer.valueOf(numbers[len-1]));
            String a = null;
            for(int i = len-2;i>=0;i--) {
                if(!stack2.isEmpty()){
                    a=stack2.peek();
                }
                //a=stack2.peek();
                System.out.println("==>"+a);

                if(a==null||pri.get(a)<pri.get(ops[i])) {
                    stack2.push(ops[i]);
                    stack1.push(Integer.valueOf(numbers[i]));
                }
                else {
                    int z =getZ(stack1, stack2);
                    stack1.push(z);
                    stack1.push(Integer.valueOf(numbers[i]));
                    stack2.push(ops[i]);
                }
            }
            while (!stack2.isEmpty()) {
                int z = getZ(stack1,stack2);
                stack1.push(z);
            }
            System.out.println(stack1.pop());
        }
    }

    public int getZ(Stack<Integer> stack1, Stack<String> stack2) {
        String t = stack2.pop();
        Integer x = stack1.pop();
        Integer y = stack1.pop();
        int z=0;
        switch (t) {
            case "+":
                z=x+y;
                break;
            case "-":
                z=x-y;
                break;
            case "*":
                z=x*y;
                break;
            case "/":
                z=x/y;
                break;
        }
        return z;
    }
}
